﻿
<!--_meta 作为公共模版分离出去-->
{include file="public/_meta"}

<article class="page-container">
	<form action="" method="get" class="form form-horizontal" id="form-admin-role-add">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>角色名称：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="title" name="title">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">备注：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="role_description" name="role_description">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">网站权限：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<dl class="permission-list">
					{volist name="data" id="vo"}
						<dt>
							<label>
								<input type="checkbox" value="{$vo.id}" name="rules[]" id="{$vo.id}" dataid="id-{$vo.id}" class="checkbox-parent">
								{$vo.title}
							</label>
						</dt>
						<dd>
						<dl class="cl permission-list2">
							{volist name="$vo['children']" id="vo2"}
								<dt>
									<label class="">
										<input type="checkbox" value="{$vo2.id}" name="rules[]" id="{$vo2.id}" dataid="id-{$vo.id}-{$vo2.id}" class="checkbox-parent checkbox-child">
										{$vo2.title}
									</label>
								</dt>
								<dd>
									{volist name="$vo2['children']" id="vo3"}
										<label class="">
											<input type="checkbox" value="{$vo3.id}" name="rules[]" id="{$vo3.id}" dataid="id-{$vo.id}-{$vo2.id}-{$vo3.id}" class="checkbox-child">
											{$vo3.title}
										</label>
									{/volist}
								</dd>
							{/volist}
						</dl>
						</dd>
					{/volist}
				</dl>
			</div>
		</div>
		<!--<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">网站权限：</label>
			<div class="formControls col-xs-8 col-sm-9">
				{volist name="rules" id="vo"}
				<div class="col-xs-3 col-md-3 col-lg-3">
					<input type="checkbox" class="input-text rule check_all" value="{$vo.id}" placeholder="" id="" name="rule[]" style="width: 15px;height: 15px;float: left;margin-top: 3px;">
					<span style="float: left;">&nbsp;&nbsp;{$vo.title}</span>
				</div>
				{/volist}
			</div>
		</div>-->
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
				<button class="btn btn-success radius check_all" type="button">全选</button>
				<button class="btn btn-success radius check_none" type="button">不选</button>
				<button class="btn btn-success radius check_reverse" type="button">反选</button>
				<button type="submit" class="btn btn-success radius" id="admin-role-add"><i class="icon-ok"></i> 添加</button>
				<button onClick="parent.layer.close(parent.layer.getFrameIndex(window.name));" class="btn btn6 size-M   radius" type="button">取消</button>
			</div>
		</div>
	</form>
</article>


<!--_footer 作为公共模版分离出去-->
{include file="public/_footer"}

<script type="text/javascript">
	/* 权限配置 */
	$(function () {
		//动态选择框，上下级选中状态变化
		$('input.checkbox-parent').on('change', function () {
			var dataid = $(this).attr("dataid");
			$('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
		});
		$('input.checkbox-child').on('change', function () {
			var dataid = $(this).attr("dataid");
			dataid = dataid.substring(0, dataid.lastIndexOf("-"));
			var parent = $('input[dataid=' + dataid + ']');
			if ($(this).is(':checked')) {
				parent.prop('checked', true);
				//循环到顶级
				while (dataid.lastIndexOf("-") != 2) {
					dataid = dataid.substring(0, dataid.lastIndexOf("-"));
					parent = $('input[dataid=' + dataid + ']');
					parent.prop('checked', true);
				}
			} else {
				//父级
				if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
					parent.prop('checked', false);
					//循环到顶级
					while (dataid.lastIndexOf("-") != 2) {
						dataid = dataid.substring(0, dataid.lastIndexOf("-"));
						parent = $('input[dataid=' + dataid + ']');
						if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
							parent.prop('checked', false);
						}
					}
				}
			}
		});
	});

	/*新增角色*/
	$("#form-admin-role-add").validate({
		rules:{
			title:{
				required:true
			}
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			/*var title = document.getElementById('title').value;
			var role_description = document.getElementById('role_description').value;
			var rule =[];
			$('input[name="rule[]"]:checked').each(function(){
				rule.push($(this).val());
			});*/
			var data = $(form).serializeArray;
			$(form).ajaxSubmit({
				url : "{:url('role/add_role')}",
				type : 'get',
				dataType : 'json',
				data : data,
				async : false,
				success : function(msg){
					if(msg.status == true){
						layer.msg(msg.message,{icon: 6,time:1000});
						setTimeout(jump,1000);
						function jump(){
							parent.location.reload();
						}
					}else if(msg.status == false){
						layer.msg(msg.message,{icon: 5,time:1000});
					}
				}
			});
			//var index = parent.layer.getFrameIndex(window.name);
		}
	});
//角色权限全选 反选 不选
$(function(){
	$(".check_all").click(function(){
		$(":checkbox").prop("checked",true);//全选
	});
	$(".check_none").click(function(){
		$(":checkbox").prop("checked",false);//取消全选
	});
	$(".check_reverse").click(function(){
		//反选
		$(":checkbox").each(function(){
			if($(this).prop("checked")){
				$(this).prop("checked",false);
			}else{
				$(this).prop("checked",true);
			}
		});
	});
});
</script>
